import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by losor on 2016/11/17.
 */
public class  _13_RomantoInteger {
    RomanDic romanDic = new RomanDic();

     public int romanToInt(String s) {
         if (s.length() == 0)
             return 0;
         int sum = 0;
         int symbol = 1;
         int lastNum = (int)romanDic.dic.get(s.charAt(0));
         for (int i = 0; i < s.length()-1; i++) {
             if((int)romanDic.dic.get(s.charAt(i))>=(int)romanDic.dic.get(s.charAt(i+1))){
                 symbol = 1;
             }
             else{
                symbol = -1;
             }
             sum += (int)romanDic.dic.get(s.charAt(i)) * symbol;
        }

        sum += (int)romanDic.dic.get(s.charAt(s.length()-1));

        return sum;
    }
}

class RomanDic{
     public Map<Character,Integer> dic = new HashMap<>();

    public RomanDic() {
        dic.put('I', 1 );
        dic.put('V', 5 );
        dic.put('X', 10);
        dic.put('L', 50);
        dic.put('C', 100);
        dic.put('D', 500);
        dic.put('M',1000);
    }
}
